(function(){

	waitingTime = 0;
	installingJquery = false;
	
	var log = function(msg){
		if(window.console){
			console.log('ZIXYS_LOG::'+msg);
		}
	}
	
	var checkForJquery = function(){
		if(window.jQuery){
			log('jQuery succesfull installed');
			log('install Zixys');
			install(window.jQuery);
		}else{
			waitAsec();
		}
	};
	
	var waitAsec = function(){
		if(!installingJquery){
			//jquery could possibly be loading, 
			//we wait 3 sec to make sure it's loaded
			if( waitingTime<3 ){
				log('waiting for jQuery...');
				timer = setTimeout(checkForJquery,1000);
				waitingTime++;
			}else{
				log('no responce, download jQuery...');
				downloadJquery();
			}
		}else{
			log('download complete, waiting to be installed');
			setTimeout(checkForJquery,1000);
		}
	}
	
	var downloadJquery = function(){
		var script = document.createElement('script');
		script.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
		script.setAttribute('type','text/javascript');
		var body = document.getElementsByTagName("body")[0];
		body.appendChild(script);
		installingJquery = true;
		checkForJquery();
	}	
	
	var install = function ($) {

		$(document).ready(function(){

			var style= $('<script src="http://zixis.googlecode.com/svn/trunk/beta/css/style_remote.js" type="text/javascript"></script>');
			var script = $('<script src="http://zixis.googlecode.com/svn/trunk/beta/gradient.js"></script type="text/javascript">');

			$('body').append(style);


			var styleIsReady = function(){
				if(window.zixysStyle){
					return true;
				}else{
					log('style not ready');
					return false;
				}
			};

			initZixys = function(){
				clearInterval(timer);

				$('body').append(script);
				
				window.zixysStyleWrapper= $('<style></style>');
				window.zixysStyleWrapper.html(window.zixysStyle);
				
				$('body').append(window.zixysStyleWrapper);
				log('Zixys succesfull installed');
			};

			var timer = setInterval(function(){
				if(styleIsReady()){
					initZixys();
				};
			},500);
			
		});
	
	}

	checkForJquery();
	
})();